package com.itheima.servlet;


import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/Hello")
public class LiveDate implements Servlet {
    //生命周期相关的练习

    @Override//用户第一次访问的执行一次
    public void init(ServletConfig servletConfig) throws ServletException {
        //1,设置网页的行内容 设置网页的编码格式,响应的类型
        System.out.println("服务器第一次被访问!");
    }

    @Override//用户每次访问都执行一次
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        String name = servletRequest.getLocalAddr();
        System.out.println("用户:" + name + "访问了服务器.");
        //1,设置网页编码格式
        servletResponse.setContentType("text/html; charset=UTF-8");
        //2,获得网页打印流
        PrintWriter responseWriter = servletResponse.getWriter();
        //3,向网页发送
        responseWriter.println("<h1 style='color:red;align:center'>这是一个黄网</h1>");
        //盗链图片
        responseWriter.println("<img src='http://tukuimg.bdstatic.com/scrop/9a8a4fbfdde5c08d950bada7d1e1aab9.gif'/>");
    }

    @Override//服务器关闭时执行
    public void destroy() {
        System.out.println("服务区关闭了!");
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }
    @Override
    public String getServletInfo() {
        return null;
    }
}
